package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.console.excel.ExportExcel;
import com.qumitech.visualnovel.console.excel.State;
import com.qumitech.visualnovel.console.excel.StateField;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.AccompanyAfterSaleAuditAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.AccompanyAppraiseAuditAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.AccompanyOrderExportDTO;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.query.AccompanyAfterSaleApplyQueryAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.query.AccompanyAppraiseQueryAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.accompany.admin.query.AccompanyOrderQueryAdminDTO;
import com.qumitech.visualnovel.service.accompany.AccompanyService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/api/accompany")
public class AccompanyController {
    @Autowired
    private AccompanyService accompanyService;

    @GetMapping("/order")
    @ExportExcel(itemCls = AccompanyOrderExportDTO.class, listKey = "orders",
        pageSize = 200,
        stateFields = {
            @StateField(key="state", states = {
                @State(state="0", display="待接单"),
                @State(state="1", display="待服务"),
                @State(state="2", display="申请开始服务"),
                @State(state="3", display="进行中"),
                @State(state="4", display="待评价"),
                @State(state="5", display="已评价"),
                @State(state="6", display="超时取消"),
                @State(state="7", display="用户主动取消"),
                @State(state="8", display="陪玩拒绝接单"),
                @State(state="9", display="退款中"),
                @State(state="10", display="申诉中"),
                @State(state="11", display="已退款"),})
        }
    )
    public BaseRsp listAccompanyOrder(AccompanyOrderQueryAdminDTO queryDTO) {
        return accompanyService.listAdminAccompanyOrder(queryDTO);
    }

    @RequestMapping(value = "/order/export", method = RequestMethod.POST)
    public BaseRsp listExportAccompanyOrder(AccompanyOrderQueryAdminDTO queryDTO,
                                            HttpServletResponse response) {
        // 全量导出，限制100w
        queryDTO.setLimit(1000000);
        queryDTO.setOffset(0);
        accompanyService.listAccompanyOrderExport(queryDTO, response);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/appeal")
    public BaseRsp listAccompanyAppeal(AccompanyAfterSaleApplyQueryAdminDTO queryDTO) {
        return accompanyService.listAdminAccompanyAfterSaleApply(queryDTO);
    }

    @GetMapping("/appraise")
    public BaseRsp listAccompanyAppraise(AccompanyAppraiseQueryAdminDTO queryDTO) {
        return accompanyService.listAccompanyAppraise(queryDTO);
    }

    @PostMapping("/order/appraise/audit")
    public BaseRsp auditAccompanyAppraise(@RequestBody AccompanyAppraiseAuditAdminDTO auditAdminDTO) {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        auditAdminDTO.setOperator(activeUser.getUsername());
        return accompanyService.auditAccompanyAppraise(auditAdminDTO);
    }

    @PostMapping("/order/{order_no}/appeal/audit")
    public BaseRsp auditAccompanyAppealOrder(@PathVariable("order_no") String orderNo,
                                             @RequestBody AccompanyAfterSaleAuditAdminDTO auditAdminDTO) {
        auditAdminDTO.setOrderNo(orderNo);
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        auditAdminDTO.setOperator(activeUser.getUsername());
        return accompanyService.auditAccompanyAppealOrder(auditAdminDTO);
    }
}
